package test.v2;

import com.baishui.beans.BeanDefinition;
import com.baishui.beans.PropertyValue;
import com.baishui.beans.factory.config.RuntimeBeanReference;
import com.baishui.beans.factory.config.TypedStringValue;
import com.baishui.beans.factory.support.DefaultBeanFactory;
import com.baishui.beans.factory.xml.XmlBeanDefinitionReader;
import com.baishui.core.io.ClassPathResource;
import com.google.gson.Gson;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;

public class Step01_BeanDefinitionTestV2 {

    /**
     * 测试 BeanDefinition
     */
	@Test
	public void testGetBeanDefinition() {
		
		DefaultBeanFactory factory = new DefaultBeanFactory();
		XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
		reader.loadBeanDefinitions(new ClassPathResource("litespring-v2.xml"));
		
		BeanDefinition bd = factory.getBeanDefinition("baishuiV2Service");
		
		List<PropertyValue> pvs = bd.getPropertyValues();
        System.out.println(new Gson().toJson(pvs));
		
		{
			PropertyValue pv = this.getPropertyValue("car", pvs);
			
			Assert.assertNotNull(pv);
			
			Assert.assertTrue(pv.getValue() instanceof RuntimeBeanReference);
		}
		
		{
			PropertyValue pv = this.getPropertyValue("name", pvs);
			
			Assert.assertNotNull(pv);

			System.out.println(((TypedStringValue)pv.getValue()).getValue());
			
			Assert.assertEquals("baishui",((TypedStringValue)pv.getValue()).getValue());
		}
		
	}

	/**
	 *  顺序的问题
	 * @param name
	 * @param pvs
	 * @return
	 */
	private PropertyValue getPropertyValue(String name,List<PropertyValue> pvs){
		for(PropertyValue pv : pvs){
			if(pv.getName().equals(name)){
				return pv;
			}
		}
		return null;
	}

}

